#Copyright (C) 2022 diff_zhang
#This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#Also add information on how to contact you by electronic and paper mail.

#!/usr/bin/env python3
import time
import sys
import xlrd
import threading
import time
import socket
import re
import os


def pinjie(protocol,wan_ip,wan_port,lan_ip,lan_port,dev_type,option):
	out = ''
	ip_for_name = wan_ip.replace('.','_')
	if dev_type == 'h3c_fw' :
		if wan_port.find('-') != -1: 
			#wan port
			ports = wan_port.split('-')
			start_port = ports[0]
			end_port = ports[1]
			#lan port
			lan_ports = lan_port.split('-')
			lan_sport = lan_ports[0]
			lan_eport = lan_ports[1]
			out += 'nat server protocol ' + protocol + ' global ' + wan_ip + \
			' ' + start_port + ' ' + end_port + ' inside ' + lan_ip + ' ' + lan_sport + ' ' + lan_eport \
			+ ' rule ' + ip_for_name + '_' + protocol + '_' + start_port + '_' + end_port + ' ' + option + '\n'
		else:
			if option == 'counting':
				out += 'nat server protocol ' + protocol + ' global ' + wan_ip + \
	' ' + wan_port + ' inside ' + lan_ip + ' ' + lan_port + ' rule ' + ip_for_name + '_' + \
	protocol + '_' + wan_port + ' ' + option + '\n'
			if option == 'reversible':
				out += 'nat server protocol ' + protocol + ' global ' + wan_ip + \
	' ' + wan_port + ' inside ' + lan_ip + ' ' + lan_port + ' reversible rule ' + ip_for_name + '_' + \
	protocol + '_' + wan_port + '\n'
			if option == 'reversible counting':
				out += 'nat server protocol ' + protocol + ' global ' + wan_ip + \
	' ' + wan_port + ' inside ' + lan_ip + ' ' + lan_port + ' reversible rule ' + ip_for_name + '_' + \
	protocol + '_' + wan_port + ' counting\n'	
	return out
def gen_natserver():
	f = xlrd.open_workbook('端口映射表.xls')
	tables = f.sheet_by_index(0)
	i = 1
	glo_out = ''
	while True:
		try:	
			index = tables.cell(i,0).value
			protocol = tables.cell(i,1).value
			wan_ip = tables.cell(i,2).value
			if not isinstance(tables.cell(i,3).value,str): 
				wan_port = str(int(tables.cell(i,3).value))  
			else:	
				wan_port = tables.cell(i,3).value
			lan_ip = tables.cell(i,4).value
			if not isinstance(tables.cell(i,5).value,str):
				lan_port = str(int(tables.cell(i,5).value))
			else:	
				lan_port = tables.cell(i,5).value			
			dev_type = tables.cell(i,6).value
			option = tables.cell(i,7).value

			if protocol == 'tcp/udp':
				protocols = protocol.split('/')
				for tcpudp in protocols:
					protocol = tcpudp
					glo_out += pinjie(protocol,wan_ip,wan_port,lan_ip,lan_port,dev_type,option)
			else:
				glo_out += pinjie(protocol,wan_ip,wan_port,lan_ip,lan_port,dev_type, option)		
			i += 1
		except IndexError:
			break
	print(glo_out)			
	with open('nat_output.txt', "w",encoding="utf-8") as f:
		f.write(glo_out)
if __name__ == "__main__":
	gen_natserver()	
	
	
	
	
	
	
	
	
	
	
	

